package com.xlh.dao.conclude.helper;

/**
 * @author cfz
 */

import cn.hutool.core.collection.CollectionUtil;
import com.xlh.dao.conclude.ConcludeCheckSubMapper;
import com.xlh.dao.conclude.ext.ConcludeCheckSubMapperExt;
import com.xlh.pojo.conclude.ConcludeCheckSub;
import com.xlh.pojo.conclude.example.ConcludeCheckSubExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author cfz
 * 课程达成度分项
 */
@Repository
public class ConcludeCheckSubMapperHelper {


    @Autowired
    private ConcludeCheckSubMapper concludeCheckSubMapper;

    @Autowired
    private ConcludeCheckSubMapperExt concludeCheckSubMapperExt;


    /**
     *
     * @param checkSubId
     * @return
     */
    public ConcludeCheckSub get(Long checkSubId) {

        ConcludeCheckSubExample concludeCheckSubExample = new ConcludeCheckSubExample();
        concludeCheckSubExample.createCriteria().andDeletedEqualTo(false).andIdEqualTo(checkSubId);
        List<ConcludeCheckSub> concludeCheckSubs = concludeCheckSubMapper.selectByExample(concludeCheckSubExample);
        if(CollectionUtil.isNotEmpty(concludeCheckSubs)){
            return concludeCheckSubs.get(0);
        }
        return null;
    }

    /**
     * 查询根据checkIds
     * @param checkIds
     * @return
     */
    public List<ConcludeCheckSub> findByCheckIds(List<Long> checkIds){

        ConcludeCheckSubExample concludeCheckSubExample = new ConcludeCheckSubExample();
        concludeCheckSubExample.createCriteria().andDeletedEqualTo(false).andCheckIdIn(checkIds);
        List<ConcludeCheckSub> concludeCheckSubs = concludeCheckSubMapper.selectByExample(concludeCheckSubExample);

        return concludeCheckSubs;
    }

    /**
     * 根据分项主键值进行删除
     * @param concludeCheckSubs
     */
    public void delete(List<ConcludeCheckSub> concludeCheckSubs) {

        ConcludeCheckSub concludeCheckSub = new ConcludeCheckSub();
        concludeCheckSub.setDeleted(true);
        List<Long> ids = concludeCheckSubs.stream().map(ConcludeCheckSub::getId).collect(Collectors.toList());
        ConcludeCheckSubExample concludeCheckSubExample = new ConcludeCheckSubExample();
        concludeCheckSubExample.createCriteria().andIdIn(ids);
        concludeCheckSubMapper.updateByExampleSelective(concludeCheckSub, concludeCheckSubExample);
    }

    /**
     * 循环插入考核分项
     * @param concludeCheckSubs
     */
    public void insertBatch(List<ConcludeCheckSub> concludeCheckSubs) {

        for (ConcludeCheckSub concludeCheckSub : concludeCheckSubs) {
            concludeCheckSubMapper.insertSelective(concludeCheckSub);
        }
    }

    /**
     * 批量更新考核分项表
     * @param updateRecords
     */
    public void updateBatch(List<ConcludeCheckSub> updateRecords) {

        for (ConcludeCheckSub updateRecord : updateRecords) {
            ConcludeCheckSubExample concludeCheckSubExample = new ConcludeCheckSubExample();
            concludeCheckSubExample.createCriteria().andIdEqualTo(updateRecord.getId());
            concludeCheckSubMapper.updateByExampleSelective(updateRecord,concludeCheckSubExample);
        }
    }


    /**
     * 更新关联sys的时候
     * @param updateRecords
     */
    public void updateBatchWithSys(List<ConcludeCheckSub> updateRecords){

        for (ConcludeCheckSub updateRecord : updateRecords) {

            concludeCheckSubMapperExt.updateByPrimaryKeySelective(updateRecord);
        }
    }


    /**
     * 批量插入数据
     * @param concludeSubs
     */
    public void insterBatch(List<ConcludeCheckSub> concludeSubs) {

        for (ConcludeCheckSub concludeSub : concludeSubs) {
            concludeCheckSubMapper.insertSelective(concludeSub);
        }
    }

    /**
     * 执行单条插入
     * @param concludeSub
     */
    public void insert(ConcludeCheckSub concludeSub) {

        concludeCheckSubMapper.insertSelective(concludeSub);
    }
}
